import md5 from "./md5.js";
//转移符转成 html
const escape2Html = function(str) {
  var arrEntities = {
    lt: "<",
    gt: ">",
    nbsp: " ",
    amp: "",
    quot: '"',
    ldquo: '"',
    rdquo: '"'
  };

  var relS = str.replace(/(lt|gt|nbsp|amp|quot|ldquo|rdquo);/g, function(
    all,
    t
  ) {
    return arrEntities[t];
  });

  return relS.replace(/&/g, "");
};
const formatTime = date => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return (
    [year, month, day].map(formatNumber).join("/") +
    " " +
    [hour, minute, second].map(formatNumber).join(":")
  );
};
// 获取本地保存的数据
const getLocalData = () => {
  try {
    return wx.getStorageSync("loginInfo");
  } catch (e) {
    console.log(e);
  }
};
const formatNumber = n => {
  n = n.toString();
  return n[1] ? n : "0" + n;
};
//获取当前时间返回格式
const getDay = () => {
  var date = new Date();
  var dateStr = formatTime(date);
  dateStr = dateStr.slice(0, 10);
  return dateStr.replace(/\//g, "");
};
// 计算tocken
const getToken = () => {
  var API_KEY = "l2V|gfZp{8`;jzR~6Y1_";
  var date = getDay();
  return md5("402268348" + date + API_KEY);
};

//格式化str file arr
const returnFileArr = fileStr => {
  return fileStr.splice("/");
};

const toMb = (limit)=>{
  return (limit / (1024 * 1024)).toFixed(4)
}  


module.exports = {
  formatTime: formatTime,
  getToken: getToken,
  getLocalData: getLocalData,
  escape2Html: escape2Html,
  toMb: toMb
};
